<?php
/**
 *
 *
 */
class ExtraController extends AppController
{
    public $name = "Extra";
    public $uses = array('Extra');



    public function extramgnt()
    {
        $this->theme = "admin";
        #$etypes = $this->Extra->get_all_etype();
        $details = $this->Extra->get_all();
        $flag = isset($this->params['pass'][0])?$this->params['pass'][0]:$this->data['flag'];
        $results = null;
        $msg = null;

        switch ($flag) {
            case "modify":
                $results = $this->Extra->get_extras_by_data($this->params['pass'][1]);
                break;

            case "search":
                $search_str = $this->data['search']['subject'];
                $results = $this->Extra->get_value(addslashes($search_str));
                self::set('search_str', $search_str);
                break;

            case "update":
                if ($this->data['extras']['extrasid'] && $this->data['extras']['value']) {
                    for ($i=0; $i < count($this->data['extras']['value']); $i++) {
                        $update_data = array('value' => $this->data['extras']['value'][$i]);
                        $update_result = $this->Extra->_save($update_data, $this->data['extras']['extrasid'][$i]);
                        if (!is_numeric($update_result)) {
                            $msg = print_r($update_result, true);
                            break;
                        }
                    }

                    if (!is_array($msg)) {
                        $msg = "Update successful";
                    }

                    if ($this->data['search']['subject']) {
                        $search_str = $this->data['search']['subject'];
                        $results = $this->Extra->get_value(addslashes($search_str));
                        self::set('search_str', $search_str);
                    }
                }
                break;
        }


        $limit = isset($this->data['limit'])?$this->data['limit']:20;
        $this->paginate['conditions'][] = array('active' => 1);
        $this->paginate['limit'] = $limit;
        $this->paginate['order'] = array('etype' => 'ASC');
        $this->paginate['group'] = array('etype');
        $etypes = $this->paginate('Extra');

        self::set('msg', $msg);
        self::set('limit', $limit);
        self::set('etypes', $etypes);
        self::set('details', json_encode($details));
        self::set('results', $results);
        self::set('cnt', count($etypes));
    }
}
?>